package com.bqtt.tumor.demo.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bqtt.tumor.demo.common.MyBeanFactory;
import com.bqtt.tumor.demo.data_access.DiagnosisMethodDAO;
import com.bqtt.tumor.demo.entity.City;
import com.bqtt.tumor.demo.entity.Desease;
import com.bqtt.tumor.demo.entity.DiagnosisMethod;

public class DiagnosisMethodService {

	private static DiagnosisMethodDAO diagnosisMethodDAO = (DiagnosisMethodDAO) MyBeanFactory.getBean("diagnosisMethodDAO");
	private static Logger log = LoggerFactory.getLogger(DiagnosisMethodService.class);
	
	public static List<DiagnosisMethod> getList()
	{
		return diagnosisMethodDAO.findAll();
	}
	
	public static List<DiagnosisMethod> searchByCondition(DiagnosisMethod condition,int pageSize,int pageNo)
	{
		
		List<DiagnosisMethod> result = diagnosisMethodDAO.fuzzyFindByProperties(condition.getSQLValidAttributes(),pageSize,pageNo);

		return result;
	}
	
	public static List<DiagnosisMethod> searchByCondition(DiagnosisMethod condition,int pageSize,int pageNo,long id)
	{
		
		List<DiagnosisMethod> result = diagnosisMethodDAO.fuzzyFindByProperties(condition.getSQLValidAttributes(),pageSize,pageNo);
		
		for(DiagnosisMethod item:result)
		{
			if(item.getDiagnosis_method_id()==id)
			{
				item.setDisplayName("inline-block");
			}
			item.fillStringField();
		}
		return result;
	}
	
	public static boolean add(DiagnosisMethod obj)
	{
		try
		{
			diagnosisMethodDAO.save(obj);
			return true;
		}
		catch(Exception e)
		{
			log.error(e.getMessage());
	     	return false;	
		}
		
	}
	
	public static boolean update(DiagnosisMethod obj)
	{
		try
		{
			 
			if(obj.getDiagnosis_method_id()>0)
			{
				diagnosisMethodDAO.update(obj);
				return true;
			}
			else
				
			return false;
		}
		catch(Exception e)
		{
			log.error(e.getMessage());
	     	return false;	
		}
		
	}
	
	
	/**
	 * @param inputFeature
	 * @return like
	 *  ['脑膜瘤',   75.0],
	 *  ['其他',   25.0]
	 */
	public static String diagnoseByClassical(List<String> inputFeatureList)
	{
		StringBuilder sb = new StringBuilder();
		List<Desease> allDeseases = DeseaseService.getDeseaseList();
		double [] deseasePercentages = new double[allDeseases.size()];
		String [] names = new String [allDeseases.size()] ;
		int index =0;
		//初始化所有疾病
		for(Desease item : allDeseases)
		{
			String name = item.getDesease_name();
			names[index] = name; 
			List<String> weakList = new ArrayList<String>();
			List<String> hardList = new ArrayList<String>();
		    String [] features = item.getClinic_feature().split("\\|");
		    
		    for(String feature : features)
		    {
		    	 if(FeatureService.getFeatureById(Integer.valueOf(feature)).getCategory() == 0)
		    	 {// hard
		    		 hardList.add(feature);
		    	 }
		    	 else
		    	 {//weak
		    		 weakList.add(feature);
		    	 }
		    }
		    
		    int score = 0;
		    int count = weakList.size()+2*hardList.size();
		    for(String inputFeautre :inputFeatureList)
		    {
		    	if(hardList.contains(inputFeautre))
		    	{
		    		score += 2;
		    	}
		    	else if(weakList.contains(inputFeautre))
		    	{
		    		score += 1;
		    	}
		    }
		    deseasePercentages[index++] = (double)score/(double)count ;
		}
		
		
		double sum = 0.0f;
		
		for(double item : deseasePercentages)
		{
			sum += item;
		}
		
		if(sum<0.0001)
		{
			sum = 1.0f;
		}
		
		index =0;
		for(double item : deseasePercentages)
		{
			 //['脑膜瘤',   75.0],
			
			String temp = String.format("%.2f", item/sum);
			temp = "['"+names[index++]+"',"+temp+"],";
			sb.append(temp);
			
		}
		
		String realResult = sb.toString();
		
		realResult.substring(0, realResult.length()-1);
		
		
		return realResult;
	}
	
}
